<?php

namespace app\admin\controller;

class Task extends Base
{
    protected function _infoModule()
    {
        return array(
            'info' => array(
                'name' => '任务库',
                'description' => '任务库模型',
            ),
            'menu' => array(
                array(
                    'name' => '列表',
                    'url' => url('index'),
                    'icon' => 'list',
                ),
            ),
            '_info' => array(
                array(
                    'name' => '添加',
                    'url' => url('info'),
                ),
            ),
        );
    }

    public function index()
    {
        $request = $this->request;

        //筛选条件
        $where = [];
        $pageMaps = [];

        $id = $request->post('id');
        $name = $request->post('name');
        $lv = $request->post('lv');
        $rwlx = $request->post('rwlx');
        $rwzl = $request->post('rwzl');

        if ($id) {
            $where[] = ['rwid','=',$id];
        }
        if ($name) {
            $where[] = ['rwname','=',$name];
        }
        if ($lv) {
            $where[] = ['rwlv','=',$lv];
        }
        if ($rwlx) {
            $where['rwlx'] = $rwlx;
        }
        if ($rwzl) {
            $where['rwzl'] = $rwzl;
        }

        //URL参数
        $pageMaps['id'] = $id;
        $pageMaps['name'] = $name;
        $pageMaps['lv'] = $lv;
        $pageMaps['rwlx'] = $rwlx;
        $pageMaps['rwzl'] = $rwzl;

        $pageAry = [
            'list_rows' => 15,
            'page' => input('page', 1),
            'path' => 'index',
            'query' => $pageMaps,
        ];

        //查询数据
        $list = model('Task')->loadList($where, $pageAry,'rwlv asc,rwid desc');
        if(!empty($list)){
            foreach ($list as $key=>$val){
                $val['lastrwid_name'] = '';
                if($val['lastrwid']>0){
                    $val['lastrwid_name'] = model('Task')->findValue(['rwid'=>$val['lastrwid']],'rwname');
                }

                //rwyq 对应模型库id(rwzl=1为道具库id)(rwzl=2为怪物库id)(rwzl=3为npc库id)
                if($val['rwzl']==1){
                    $rwyq_name = model('Prop')->findValue(['djid'=>$val['rwyq']],'djname');
                    $val['rwyq_name'] = $rwyq_name . ' x ' . $val['rwcount'];
                }
                if($val['rwzl']==2){
                    $rwyq_name = model('Monster')->findValue(['id'=>$val['rwyq']],'gname');
                    $val['rwyq_name'] = $rwyq_name . ' x ' . $val['rwcount'];
                }
                if($val['rwzl']==3){
                    $rwyq_name = model('Npc')->findValue(['id'=>$val['rwyq']],'nname');
                    $rwcount_name = model('Npc')->findValue(['id'=>$val['rwcount']],'nname');
                    $val['rwyq_name'] = $rwyq_name . ' -> ' . $rwcount_name;
                }

                //奖励
                $val['rwzb_name'] = $this->strSelect($val['rwzb'],1);
                $val['rwdj_name'] = $this->strSelect($val['rwdj'],2);
                $val['rwyp_name'] = $this->strSelect($val['rwyp'],3);
                $val['rwjn_name'] = model('Skill')->findValue(['jnid'=>$val['rwjineng']],'jnname');
            }
        }
        $this->assign('list', $list);
        $this->assign('_page', $list->render());
        $this->assign('pageMaps', $pageMaps);

        return $this->fetch('index');
    }

    public function strSelect($strIds,$type){
        $ary = ($strIds != '' && $strIds != null) ? explode(',', $strIds) : [];
        $list = [];
        if (!empty($ary)) {
            foreach ($ary as $k => $item) {
                $aryInfo = explode('|', $item);

                $name = '';
                if($type==1){
                    $name = model('Equip')->findValue(['zbid'=>$aryInfo[0]],'zbname');
                    $list[$k]['lv'] = model('Equip')->findValue(['zbid'=>$aryInfo[0]],'zblv');
                }
                if($type==2){
                    $name = model('Prop')->findValue(['djid'=>$aryInfo[0]],'djname');
                }
                if($type==3){
                    $name = model('Drug')->findValue(['ypid'=>$aryInfo[0]],'ypname');
                    $list[$k]['lv'] = model('Drug')->findValue(['ypid'=>$aryInfo[0]],'yplv');
                }

                $list[$k]['id'] = $aryInfo[0];
                $list[$k]['num'] = empty($aryInfo[1]) ? 1 : $aryInfo[1];
                $list[$k]['name'] = $name;
            }
        }
        return $list;
    }

    public function info()
    {
        $request = request();
        $rwid = input('rwid');
        $model = model('Task');
        if ($request->method() == 'POST') {
            $rwzb = '';
            if (!empty($_POST['rwzb'])){
                //预处理操作方法数据
                $rwzb = $this->getAryStr2($_POST['rwzb']);
            }
            $_POST['rwzb'] = $rwzb;
            
            $rwdj = '';
            if (!empty($_POST['rwdj'])){
                //预处理操作方法数据
                $rwdj = $this->getAryStr($_POST['rwdj']);
            }
            $_POST['rwdj'] = $rwdj;

            $rwyp = '';
            if (!empty($_POST['rwyp'])){
                //预处理操作方法数据
                $rwyp = $this->getAryStr($_POST['rwyp']);
            }
            $_POST['rwyp'] = $rwyp;

            if ($rwid) {
                $status = $model->editAll($request);
            } else {
                $status = $model->addall($request);
            }
            if ($status !== false) {
                returnJson(1, '操作成功', ['url' => url('index')]);
            } else {
                returnJson(0, '操作失败');
            }
        } else {
            $info = $model->getInfo($rwid);
            if (!empty($info)) {
                if ($info['rwzl']==1) {
                    $info['rwyq_name'] = model('Prop')->findValue(['djid'=>$info['rwyq']],'djname');
                }
                if ($info['rwzl']==2) {
                    $info['rwyq_name'] = model('Monster')->findValue(['id'=>$info['rwyq']],'gname');
                }
                if ($info['rwzl']==3) {
                    $info['rwyq_name'] = model('Npc')->findValue(['id'=>$info['rwyq']],'nname');
                    $info['rwcount_name'] = model('Npc')->findValue(['id'=>$info['rwcount']],'nname');
                }

                $info['rwzb_list'] = $this->strSelect($info['rwzb'],1);
                $info['rwdj_list'] = $this->strSelect($info['rwdj'],2);
                $info['rwyp_list'] = $this->strSelect($info['rwyp'],3);
            }
            $this->assign('info', $info);
            //echo "<pre>";print_r($_POST);die;

            $rwzb_list = empty($info) ? 1 : count($info['rwzb_list']) + 1;
            $this->assign('rwzb_list', $rwzb_list);
            $rwdj_list = empty($info) ? 1 : count($info['rwdj_list']) + 1;
            $this->assign('rwdj_list', $rwdj_list);
            $rwyp_list = empty($info) ? 1 : count($info['rwyp_list']) + 1;
            $this->assign('rwyp_list', $rwyp_list);

            $listSkill = model('Skill')->getList();
            $this->assign('listSkill', $listSkill);

            $this->fetch('info');
        }
    }

    public function getFrontTask()
    {
        $where = [];
        $ulv = request()->get('ulv');
        if ($ulv) {
            $where[] = ['rwlv','<=',$ulv];
        }

        $list = model('Task')->getList($where);
        if (!empty($list)) {
            foreach ($list as $k => $item) {
                $item['rwlx'] = $item['rwlx'] == 1 ? "普通" : ($item['rwlx'] == 2 ? '每日' : '主线');
                $item['rwzl'] = $item['rwzl'] == 1 ? "收集" : ($item['rwzl'] == 2 ? '打怪' : '对话');
            }
        }
        $this->assign('list', $list);
        return $this->fetch('task');
    }

    public function getMoldList()
    {
        $list = [];
        $type = input('type');
        if ($type==1) {
            $list = model('Equip')->field('zbid as id,zbname as name,zblv as lv')->order('zblv asc,zbid desc')->select();
        }
        if ($type==2) {
            $list = model('Prop')->field('djid as id,djname as name')->order('djid desc')->select();
            foreach ($list as $item){
                $item['lv'] = '';
            }
        }
        if ($type==3) {
            $list = model('Drug')->field('ypid as id,ypname as name')->order('ypid desc')->select();
            foreach ($list as $item){
                $item['lv'] = '';
            }
        }
        if ($type==4) {
            $list = model('Monster')->field('id,gname as name,glv as lv')->order('glv asc,id desc')->select();
        }
        if ($type==5) {
            $list = model('Npc')->field('id,nname as name,nlv as lv')->order('nlv asc,id desc')->select();
        }
        $this->assign('list', $list);

        return $this->fetch('mold');
    }

    protected function getAryStr($data)
    {
        $ary = [];
        if(!empty($data)){
            foreach($data as $key=>$vo){
                $ary[$key] = $vo['id'] . '|' . $vo['num'];
            }
        }
        return implode(',',$ary);
    }
    protected function getAryStr2($data)
    {
        $ary = [];
        if(!empty($data)){
            foreach($data as $key=>$vo){
                $ary[$key] = $vo['id'];
            }
        }
        return implode(',',$ary);
    }

    /**
     * 删除
     */
    public function del()
    {
        $request = $this->request;
        $id = $request->post('id');
        if (empty($id)) {
            returnJson(0, '参数不能为空');
        }

        if (model('Task')->del($id)) {
            returnJson(1, '删除成功！');
        } else {
            returnJson(0, '删除失败');
        }
    }
}

